﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace WindowsFormsApp1
{
    public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
        }
        public Add(int id,string name,string passWord)
        {
            InitializeComponent();

            this.id = id;  
            textUserName.Text = name;
            textPassWord.Text = passWord;

            
        }
        private int id;
        private void button1_Click(object sender, EventArgs e)
        {

            var name = textUserName.Text;
            var passWord = textPassWord.Text;
            ///更新//
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var table=string.Format("update Users set UserName= '{0}',PassWord='{1}'where Id={2}",name,passWord,this.id);

                    var update = DbHelper.AddOrDelete(table);

                    if (update == 1)
                    {
                        MessageBox.Show("更新成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增//
            else
            {              
                if (!string.IsNullOrEmpty(name))
                {
                    var table = string.Format("insert into Users(UserName,PassWord)values('{0}','{1}')", name, passWord);

                    var update = DbHelper.AddOrDelete(table);

                    if (update == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    //else
                    //{
                    //    MessageBox.Show("添加失败", "提示");
                    //    this.Close();
                    //}

                }
                else
                {
                    MessageBox.Show("姓名不能为空！", "提示");
                }
              
                

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.No;
        }
    }
}
